package com.cd.dao;

import com.cd.entity.TV;
import com.cd.entity.TVCategory;
import com.cd.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class TVDao {
    // 1. 查询所有电视剧
    public List<TV> findAll() throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.selectList("com.cd.dao.TVMapper.findAll");
        }
    }

    // 2. 根据ID查询电视剧
    public TV findById(Integer tvId) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.selectOne("com.cd.dao.TVMapper.findById", tvId);
        }
    }

    // 3. 根据分类ID查询电视剧
    public List<TV> findByCategoryId(Integer categoryId) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.selectList("com.cd.dao.TVMapper.findByCategoryId", categoryId);
        }
    }

    // 4. 插入电视剧
    public int insert(TV tv) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.insert("com.cd.dao.TVMapper.insert", tv);
        }
    }

    // 5. 更新电视剧
    public int update(TV tv) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.update("com.cd.dao.TVMapper.update", tv);
        }
    }

    // 6. 删除电视剧
    public int delete(Integer tvId) throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.delete("com.cd.dao.TVMapper.delete", tvId);
        }
    }

    // 7. 查询电视剧及其分类信息
    public List<TV> findAllWithCategory() throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            return session.selectList("com.cd.dao.TVMapper.findAllWithCategory");
        }
    }
}